package com.syqc.comlib.utils

import android.app.Activity
import android.content.Context
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.WindowManager

object ScreenUtil {
    /**
     * 获取屏幕内容高度
     *
     * @param activity
     * @return
     */
    fun getScreenHeight(activity: Activity): Int {
//        val dm = DisplayMetrics()
//        activity.windowManager.defaultDisplay.getMetrics(dm)
//        var result = 0
//        val resourceId =
//            activity.resources.getIdentifier("status_bar_height", "dimen", "android")
//        if (resourceId > 0) {
//            result = activity.resources.getDimensionPixelSize(resourceId)
//        }

        val windowManager = activity.getSystemService(Context.WINDOW_SERVICE) as? WindowManager
        if (windowManager != null) {
            val outMetrics = DisplayMetrics()
            windowManager.defaultDisplay.getRealMetrics(outMetrics)
            return outMetrics.heightPixels
        }
        return 1920
    }

    fun dp2px(context: Context, dp: Int): Int {
        val dm: DisplayMetrics = context.resources.displayMetrics
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp.toFloat(), dm).toInt()
    }

//    fun dpToPx(c: Context, dipValue: Float): Int {
//        val metrics = c.resources.displayMetrics
//        val `val` = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics)
//        val res = (`val` + 0.5).toInt() // Round
//        // Ensure at least 1 pixel if val was > 0
//        return if (res == 0 && `val` > 0) 1 else res
//    }
}